package com.eyas.im.protocol.encoder;

import com.eyas.im.protocol.protocol.StringProtocol;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class StringEncoder implements Encoder<StringProtocol> {
    @Override
    public byte[] encode(StringProtocol msg) throws EncoderFailException {
        if(msg.getMessage().size()==0)
            throw new IllegalArgumentException("发送的数据为空");
        ByteArrayOutputStream byteChannel = new ByteArrayOutputStream();
        for(String oneMsg:msg.getMessage()) {
            try {
                byteChannel.write(oneMsg.getBytes(StandardCharsets.UTF_8));
            } catch (IOException e) {
                throw new EncoderFailException("协议编码失败",e);
            }
        }
        return byteChannel.toByteArray();
    }
}
